自定义指令实现点击水波蔓延效果
实现原理
点击时候获取鼠标位置,创建一个span标签,执行向外扩散,透明度由1变为0的动画
定义js文件1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26const ripple = (el) => {
var addRippleEffect = function (e) {
// var target = e.target
var target = el
// if (target.className.toLowerCase() !== 'btn') return false
target.style.overflow = 'hidden'
var rect = target.getBoundingClientRect()
var ripple = target.querySelector('.ripple')
if (!ripple) {
ripple = document.createElement('span')
ripple.className = 'ripple'
ripple.style.height = ripple.style.width = Math.max(rect.width, rect.height) + 'px'
target.appendChild(ripple)
}
ripple.classList.remove('show')
var top = e.pageY - rect.top - ripple.offsetHeight / 2 - document.body.scrollTop
var left = e.pageX - rect.left - ripple.offsetWidth / 2 - document.body.scrollLeft
ripple.style.top = top + 'px'
ripple.style.left = left + 'px'
ripple.classList.add('show')
return false
}
el.addEventListener('click', addRippleEffect, false)
}
export default ripple
定义样式,需全局
1 | .ripple |
main.js中 引入并注册指令
1 | import Ripple from '@/assets/utils/ripple' |
使用
1 | .item(v-ripple="") |